package
{
	import flash.display.*;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.filters.BevelFilter;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.BitmapFilterType;
	import flash.filters.BlurFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.FileFilter;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.utils.getDefinitionByName;
	import flash.utils.getTimer;
	
	public class RotatePuzzle extends MovieClip
	{	
		public static const XCount:uint = 6;
		public static const YCount:uint = 4;
		public static const CeilSize:uint = 80;
		public static const boardOffsetX:uint = 80;
		public static const boardOffsetY:uint = 80;
		
		private var dialog:Class = getDefinitionByName("Dialog") as Class;	//对话框
		private var bitmapDataSrc:BitmapData;	//获取源位图
		private var bmSprite:Sprite;
		private var bitmapSmallArray:Array;	//存储切开的小位图
		private var sourceImage:Bitmap;
		
		
		//效果
		private var bevelFilter:BevelFilter = new BevelFilter();	//滤镜
		
		public function RotatePuzzle()
		{
			//initGame();
			//startGame();
		}
		
		public function initGame():void{
			//	stage.align = StageAlign.TOP;
			//	MovieClip(root).stage.scaleMode = StageScaleMode.NO_SCALE;
			//stage.scaleMode = StageScaleMode.NO_SCALE;
		}
		
		
		/* start the game */
		public function startGame():void{
			loadImage();
			
		}
		
		private function gameLoop(event:Event):void{
			var rotationTotal:int = 0;
			for(var i:int = 0; i < bitmapSmallArray.length;i++){
				var bmDiamond:BMDiamond = bitmapSmallArray[i] as BMDiamond;
				rotationTotal += Math.abs(bmDiamond.rotation);	
			}
			
			if(rotationTotal == 0 && getTimer() > 500){
				endGame();
			}

		}
		
		/* load the bitmap */
		public function loadImage():void{
			var loader:Loader = new Loader();
			var request:URLRequest = new URLRequest("dandan.png");
			loader.load(request);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderBitmapListener);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loaderIOErrorListener);
		}
		
		// 加载图片完成的响应事件
		private function loaderBitmapListener(event:Event):void{
			var loader:Loader = event.target.loader as Loader;
			sourceImage = Bitmap(loader.content);
			
			bitmapDataSrc = sourceImage.bitmapData;
			bitmapSmallArray = new Array();
			initEffet();
			for(var i:uint = 0;i < XCount; i++){
				for(var j:uint = 0;j < YCount; j++){				
					var bitDataSmall:BitmapData = new BitmapData(CeilSize,CeilSize);
					var rect:Rectangle = new Rectangle(i*CeilSize,j*CeilSize,CeilSize,CeilSize);
					var pt:Point = new Point(0,0);
					bitDataSmall.copyPixels(bitmapDataSrc,rect,pt);
					var bmdSprite:BMDiamond = new BMDiamond(bitDataSmall);
						
					bmdSprite.x = boardOffsetX + i*CeilSize;
					bmdSprite.y =  boardOffsetY + j*CeilSize;
					bmdSprite.width = CeilSize;
					bmdSprite.height = CeilSize;
				//	trace(bmdSprite.x  + " " + bmdSprite.y );
					
					//添加小切块的效果
					bmdSprite.alpha = 0.8;
					bmdSprite.filters = [bevelFilter];
					addChild(bmdSprite);
					
					var random:int = Math.floor(Math.random() * 4);		//返回随机数0到 3
					bmdSprite.rotation = 90 * random;
					bitmapSmallArray.push(bmdSprite);
				}
			}
			
			this.addEventListener(Event.ENTER_FRAME,gameLoop);
			
		}
		
		// 加载图片错误的提示
		private function loaderIOErrorListener(event:Event):void{
			trace("Error to load the image!");
		}
		
		public function endGame():void{
			
			removeEventListener(Event.ENTER_FRAME,gameLoop);	//删除监听
			for(var i:int = 0; i < bitmapSmallArray.length; i++){
				(bitmapSmallArray[i] as BMDiamond).alpha = 1;
			}
		
			gotoAndStop("gameover");
			showDialog();
		}
		
		//清楚舞台上的元件
		public function clearUp():void{
			
			bitmapDataSrc.dispose();
			for(var i:int = 0; i < bitmapSmallArray.length; i++){
				removeChild(bitmapSmallArray[i]);
			}
			
			bitmapSmallArray = null;
		}
		
		// 弹出提示对话框 
		public function showDialog():void{
			
			var tipsDialog:MovieClip = new dialog();
			tipsDialog.message.text = "Good done！";
			tipsDialog.x = 180;
			tipsDialog.y = 140;
			addChild(tipsDialog);
		}
		
		public function clickDialogButton(event:MouseEvent):void{
			removeChild(MovieClip(event.currentTarget.parent));
			clearUp();
			gotoAndStop("startgame");
		}
		
		private function initEffet():void{
			bevelFilter.angle = 45;
			bevelFilter.distance = 2;
			bevelFilter.shadowColor = 0xffffff
			//bevelFilter.shadowAlpha = 0.5;
			bevelFilter.highlightColor = 0xffff00;
			bevelFilter.highlightAlpha = 0.5;
			bevelFilter.blurX = 2;
			bevelFilter.blurY = 2;
			bevelFilter.type = BitmapFilterType.FULL;
		}
	}
}